home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / machine / gaplus.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  19KB  |  679 lines

  1. /***************************************************************************
  2.  
  3.   machine.c
  4.  
  5.   Functions to emulate general aspects of the machine (RAM, ROM, interrupts,
  6.   I/O ports)
  7.  
  8. ***************************************************************************/
  9.  
  10. #include "driver.h"
  11. #include "cpu/m6809/m6809.h"
  12.  
  13. unsigned char *gaplus_snd_sharedram;
  14. unsigned char *gaplus_sharedram;
  15. unsigned char *gaplus_customio_1,*gaplus_customio_2,*gaplus_customio_3;
  16. static int int_enable_2, int_enable_3;
  17. static int credits, coincounter1, coincounter2;
  18.  
  19. extern void gaplus_flipscreen_w( int data );
  20. extern void gaplus_starfield_update( void );
  21.  
  22. void gaplus_init_machine( void )
  23. {
  24.     int_enable_2 = int_enable_3 = 1;
  25.     credits = coincounter1 = coincounter2 = 0;
  26. }
  27.  
  28. /* shared ram functions */
  29. READ_HANDLER( gaplus_sharedram_r )
  30. {
  31.     return gaplus_sharedram[offset];
  32. }
  33.  
  34. WRITE_HANDLER( gaplus_sharedram_w )
  35. {
  36.     if (offset == 0x082c)    /* 0x102c */
  37.         gaplus_flipscreen_w( data );
  38.     gaplus_sharedram[offset] = data;
  39. }
  40.  
  41. READ_HANDLER( gaplus_snd_sharedram_r )
  42. {
  43.     return gaplus_snd_sharedram[offset];
  44. }
  45.  
  46. WRITE_HANDLER( gaplus_snd_sharedram_w )
  47. {
  48.     gaplus_snd_sharedram[offset] = data;
  49. }
  50.  
  51. /* irq control functions */
  52. WRITE_HANDLER( gaplus_interrupt_ctrl_2_w )
  53. {
  54.     int_enable_2 = offset;
  55. }
  56.  
  57. WRITE_HANDLER( gaplus_interrupt_ctrl_3a_w )
  58. {
  59.     int_enable_3 = 1;
  60. }
  61.  
  62. WRITE_HANDLER( gaplus_interrupt_ctrl_3b_w )
  63. {
  64.     int_enable_3 = 0;
  65. }
  66.  
  67. int gaplus_interrupt_1( void ) {
  68.  
  69.     gaplus_starfield_update(); /* update starfields */
  70.  
  71.     return interrupt();
  72. }
  73.  
  74. int gaplus_interrupt_2( void )
  75. {
  76.     if (int_enable_2)
  77.         return interrupt();
  78.     else
  79.         return ignore_interrupt();
  80. }
  81.  
  82. int gaplus_interrupt_3( void )
  83. {
  84.     if (int_enable_3)
  85.         return interrupt();
  86.     else
  87.         return ignore_interrupt();
  88. }
  89.  
  90. WRITE_HANDLER( gaplus_reset_2_3_w )
  91. {
  92.     int_enable_2 = int_enable_3 = 1;
  93.     cpu_set_reset_line(1,PULSE_LINE);
  94.     cpu_set_reset_line(2,PULSE_LINE);
  95.     credits = coincounter1 = coincounter2 = 0;
  96. }
  97.  
  98. /************************************************************************************
  99. *                                                                                    *
  100. *           Gaplus custom I/O chips (preliminary)                                    *
  101. *                                                                                    *
  102. ************************************************************************************/
  103.  
  104. WRITE_HANDLER( gaplus_customio_1_w )
  105. {
  106.     gaplus_customio_1[offset] = data;
  107. }
  108.  
  109. WRITE_HANDLER( gaplus_customio_2_w )
  110. {
  111.     gaplus_customio_2[offset] = data;
  112. }
  113.  
  114. WRITE_HANDLER( gaplus_customio_3_w )
  115. {
  116.     if ((offset == 0x09) && (data >= 0x0f))
  117.         sample_start(0,0,0);
  118.     gaplus_customio_3[offset] = data;
  119. }
  120.  
  121. static int credmoned [] = { 1, 1, 2, 3 };
  122. static int monedcred [] = { 1, 2, 1, 1 };
  123.  
  124. READ_HANDLER( gaplus_customio_1_r )
  125. {
  126.     int mode, val, temp1, temp2;
  127.  
  128.     mode = gaplus_customio_1[8];
  129.     if (mode == 3)    /* normal mode */
  130.     {
  131.         switch (offset)
  132.         {
  133.             case 0:     /* Coin slots, high nibble of port 2 */
  134.             {
  135.                 static int lastval;
  136.  
  137.                 val = readinputport( 2 ) >> 4;
  138.                 temp1 = readinputport( 0 ) & 0x03;
  139.                 temp2 = (readinputport( 0 ) >> 6) & 0x03;
  140.  
  141.                 /* bit 0 is a trigger for the coin slot 1 */
  142.                 if ((val & 1) && ((val ^ lastval) & 1))
  143.                 {
  144.                     coincounter1++;
  145.                     if (coincounter1 >= credmoned[temp1])
  146.                     {
  147.                         credits += monedcred [temp1];
  148.                         coincounter1 -= credmoned [temp1];
  149.                     }
  150.                 }
  151.                 /* bit 1 is a trigger for the coin slot 2 */
  152.                 if ((val & 2) && ((val ^ lastval) & 2))
  153.                 {
  154.                     coincounter2++;
  155.                     if (coincounter2 >= credmoned[temp2])
  156.                     {
  157.                         credits += monedcred [temp2];
  158.                         coincounter2 -= credmoned [temp2];
  159.                     }
  160.                 }
  161.  
  162.                 if (credits > 99)
  163.                     credits = 99;
  164.  
  165.                 return lastval = val;
  166.             }
  167.                 break;
  168.             case 1:
  169.             {
  170.                 static int lastval;
  171.  
  172.                 val = readinputport( 2 ) & 0x03;
  173.                 temp1 = readinputport( 0 ) & 0x03;
  174.                 temp2 = (readinputport( 0 ) >> 6) & 0x03;
  175.  
  176.                 /* bit 0 is a trigger for the 1 player start */
  177.                 if ((val & 1) && ((val ^ lastval) & 1))
  178.                 {
  179.                     if (credits > 0)
  180.                         credits--;
  181.                     else
  182.                         val &= ~1;   /* otherwise you can start with no credits! */
  183.                 }
  184.                 /* bit 1 is a trigger for the 2 player start */
  185.                 if ((val & 2) && ((val ^ lastval) & 2))
  186.                 {
  187.                     if (credits >= 2)
  188.                         credits -= 2;
  189.                     else
  190.                         val &= ~2;   /* otherwise you can start with no credits! */
  191.                 }
  192.                 return lastval = val;
  193.             }
  194.                 break;
  195.             case 2:
  196.                 return (credits / 10);      /* high BCD of credits */
  197.                 break;
  198.             case 3:
  199.                 return (credits % 10);      /* low BCD of credits */
  200.                 break;
  201.             case 4:
  202.                 return (readinputport( 3 ) & 0x0f);   /* 1P controls */
  203.                 break;
  204.             case 5:
  205.                 return (readinputport( 4 ) & 0x03);   /* 1P button 1 */
  206.                 break;
  207.             case 6:
  208.                 return (readinputport( 3 ) >> 4);     /* 2P controls */
  209.                 break;
  210.             case 7:
  211.                 return ((readinputport( 4 ) >> 2) & 0x03);    /* 2P button 1 */
  212.                 break;
  213.             default:
  214.                 return gaplus_customio_1[offset];
  215.         }
  216.     }
  217.     else if (mode == 5)  /* IO tests chip 1 */
  218.     {
  219.         switch (offset)
  220.         {
  221.             case 0:
  222.             case 1:
  223.                 return 0x0f;
  224.                 break;
  225.             default:
  226.                 return gaplus_customio_1[offset];
  227.         }
  228.     }
  229.     else if (mode == 1)    /* test mode controls */
  230.     {
  231.         switch (offset)
  232.         {
  233.             case 4:
  234.                 return (readinputport( 2 ) & 0x03);    /* start 1 & 2 */
  235.                 break;
  236.             case 5:
  237.                 return (readinputport( 3 ) &0x0f);    /* 1P controls */
  238.                 break;
  239.             case 6:
  240.                 return (readinputport( 3 ) >> 4);    /* 2P controls */
  241.                 break;
  242.             case 7:
  243.                 return (readinputport( 4 ) & 0x0f);    /* button 1 & 2 */
  244.                 break;
  245.             default:
  246.                 return gaplus_customio_1[offset];
  247.         }
  248.     }
  249.     return gaplus_customio_1[offset];
  250. }
  251. READ_HANDLER( gaplus_customio_2_r )
  252. {
  253.     int val, mode;
  254.  
  255.     mode = gaplus_customio_2[8];
  256.     if (mode == 8)  /* IO tests chip 2 */
  257.     {
  258.         switch (offset)
  259.         {
  260.             case 0:
  261.                 return 0x06;
  262.                 break;
  263.             case 1:
  264.                 return 0x09;
  265.                 break;
  266.             default:
  267.                 return gaplus_customio_2[offset];
  268.         }
  269.     }
  270.     else    if (mode == 1)    /* this values are read only by the game on power up */
  271.     {
  272.         switch (offset)
  273.         {
  274.             case 0:
  275.                 val = readinputport( 0 ) & 0x0f; /* credits/coin 1P & fighters */
  276.                 break;
  277.             case 1:
  278.                 val = readinputport( 1 ) >> 5;   /* bonus life */
  279.                 break;
  280.             case 2:
  281.                 val = readinputport( 1 ) & 0x0f; /* rank & test mode */
  282.                 break;
  283.             case 3:
  284.                 val = readinputport( 0 ) >> 6;   /* credits/coin 2P */
  285.                 break;
  286.             default:
  287.                 val = gaplus_customio_2[offset];
  288.         }
  289.         return val;
  290.     }
  291.     else
  292.         return gaplus_customio_2[offset];
  293. }
  294.  
  295. READ_HANDLER( gaplus_customio_3_r )
  296. {
  297.     int mode;
  298.  
  299.     mode = gaplus_customio_3[8];
  300.     if (mode == 2)
  301.     {
  302.         switch (offset)
  303.         {
  304.             case 2:
  305.                 return 0x0f;
  306.                 break;
  307.             default:
  308.                 return gaplus_customio_3[offset];
  309.         }
  310.     }
  311.     else
  312.     {
  313.         switch (offset)
  314.         {
  315.             case 0:
  316.                 return ((readinputport( 0 ) & 0x20) >> 3);   /* cabinet */
  317.                 break;
  318.             case 1:
  319.                 return 0x0f;
  320.                 break;
  321.             case 2:
  322.                 return 0x0e;
  323.                 break;
  324.             case 3:
  325.                 return 0x01;
  326.                 break;
  327.             default:
  328.                 return gaplus_customio_3[offset];
  329.         }
  330.     }
  331. }
  332.  
  333.  
  334. /************************************************************************************
  335. *                                                                                    *
  336. *           Gaplus (set 2) custom I/O chips (preliminary)                                    *
  337. *                                                                                    *
  338. ************************************************************************************/
  339.  
  340. READ_HANDLER( gaplusa_customio_1_r )
  341. {
  342.     int mode, val, temp1, temp2;
  343.  
  344.     mode = gaplus_customio_1[8];
  345.     if (mode == 4)    /* normal mode */
  346.     {
  347.         switch (offset)
  348.         {
  349.         case 0:
  350.             return (credits / 10);      /* high BCD of credits */
  351.             break;
  352.         case 1:
  353.             return (credits % 10);      /* low BCD of credits */
  354.             break;
  355.         case 2:     /* Coin slots, high nibble of port 2 */
  356.             {
  357.                 static int lastval;
  358.  
  359.                 val = readinputport( 2 ) >> 4;
  360.                 temp1 = readinputport( 0 ) & 0x03;
  361.                 temp2 = (readinputport( 0 ) >> 6) & 0x03;
  362.  
  363.                 /* bit 0 is a trigger for the coin slot 1 */
  364.                 if ((val & 1) && ((val ^ lastval) & 1))
  365.                 {
  366.                     coincounter1++;
  367.                     if (coincounter1 >= credmoned[temp1])
  368.                     {
  369.                         credits += monedcred [temp1];
  370.                         coincounter1 -= credmoned [temp1];
  371.                     }
  372.                 }
  373.                 /* bit 1 is a trigger for the coin slot 2 */
  374.                 if ((val & 2) && ((val ^ lastval) & 2))
  375.                 {
  376.                     coincounter2++;
  377.                     if (coincounter2 >= credmoned[temp2])
  378.                     {
  379.                         credits += monedcred [temp2];
  380.                         coincounter2 -= credmoned [temp2];
  381.                     }
  382.                 }
  383.  
  384.                 if (credits > 99)
  385.                     credits = 99;
  386.  
  387.                 return lastval = val;
  388.             }
  389.                 break;
  390.             case 3:
  391.             {
  392.                 static int lastval;
  393.  
  394.                 val = readinputport( 2 ) & 0x03;
  395.                 temp1 = readinputport( 0 ) & 0x03;
  396.                 temp2 = (readinputport( 0 ) >> 6) & 0x03;
  397.  
  398.                 /* bit 0 is a trigger for the 1 player start */
  399.                 if ((val & 1) && ((val ^ lastval) & 1))
  400.                 {
  401.                     if (credits > 0)
  402.                         credits--;
  403.                     else
  404.                         val &= ~1;   /* otherwise you can start with no credits! */
  405.                 }
  406.                 /* bit 1 is a trigger for the 2 player start */
  407.                 if ((val & 2) && ((val ^ lastval) & 2))
  408.                 {
  409.                     if (credits >= 2)
  410.                         credits -= 2;
  411.                     else
  412.                         val &= ~2;   /* otherwise you can start with no credits! */
  413.                 }
  414.                 return lastval = val;
  415.             }
  416.                 break;
  417.             case 4:
  418.                 return (readinputport( 3 ) & 0x0f);   /* 1P controls */
  419.                 break;
  420.             case 5:
  421.                 return (readinputport( 4 ) & 0x03);   /* 1P button 1 */
  422.                 break;
  423.             case 6:
  424.                 return (readinputport( 3 ) >> 4);     /* 2P controls */
  425.                 break;
  426.             case 7:
  427.                 return ((readinputport( 4 ) >> 2) & 0x03);    /* 2P button 1 */
  428.                 break;
  429.             default:
  430.                 return gaplus_customio_1[offset];
  431.         }
  432.     }
  433.     else if (mode == 8)  /* IO tests chip 1 */
  434.     {
  435.         switch (offset)
  436.         {
  437.             case 0:
  438.                 return 0x06;
  439.                 break;
  440.             case 1:
  441.                 return 0x09;
  442.                 break;
  443.             default:
  444.                 return gaplus_customio_1[offset];
  445.         }
  446.     }
  447.     else if (mode == 1)    /* test mode */
  448.     {
  449.         switch (offset)
  450.         {
  451.         case 0:
  452.             return (readinputport( 2 ) & 0x03);    /* start 1 & 2 */
  453.             break;
  454.         case 1:
  455.             return (readinputport( 3 ) &0x0f);    /* 1P controls */
  456.             break;
  457.         case 2:
  458.             return (readinputport( 3 ) >> 4);    /* 2P controls */
  459.             break;
  460.         case 3:
  461.             return (readinputport( 4 ) & 0x0f);    /* button 1 & 2 */
  462.             break;
  463.         default:
  464.             return gaplus_customio_1[offset];
  465.         }
  466.     }
  467.     return gaplus_customio_1[offset];
  468. }
  469. READ_HANDLER( gaplusa_customio_2_r )
  470. {
  471.     int val, mode;
  472.  
  473.     mode = gaplus_customio_2[8];
  474.     if (mode == 5)  /* IO tests chip 2 */
  475.     {
  476.         switch (offset)
  477.         {
  478.             case 0:
  479.             case 1:
  480.                 return 0x0f;
  481.                 break;
  482.             default:
  483.                 return gaplus_customio_2[offset];
  484.         }
  485.     }
  486.     else    if (mode == 4)    /* this values are read only by the game on power up */
  487.     {
  488.         switch (offset)
  489.         {
  490.             case 1:
  491.                 val = readinputport( 0 ) & 0x0f; /* credits/coin 1P & fighters */
  492.                 break;
  493.             case 2:
  494.                 val = readinputport( 1 ) >> 5;   /* bonus life */
  495.                 break;
  496.             case 4:
  497.                 val = readinputport( 1 ) & 0x0f; /* rank & test mode */
  498.                 break;
  499.             case 7:
  500.                 val = readinputport( 0 ) >> 6;   /* credits/coin 2P */
  501.                 break;
  502.             default:
  503.                 val = gaplus_customio_2[offset];
  504.         }
  505.         return val;
  506.     }
  507.     else
  508.         return gaplus_customio_2[offset];
  509. }
  510.  
  511. READ_HANDLER( gaplusa_customio_3_r )
  512. {
  513.     int mode;
  514.  
  515.     mode = gaplus_customio_3[8];
  516.     if (mode == 2)
  517.     {
  518.         switch (offset)
  519.         {
  520.             case 2:
  521.                 return 0x0f;
  522.                 break;
  523.             default:
  524.                 return gaplus_customio_3[offset];
  525.         }
  526.     }
  527.     else
  528.     {
  529.         switch (offset)
  530.         {
  531.             case 0:
  532.                 return ((readinputport( 0 ) & 0x20) >> 3);   /* cabinet */
  533.                 break;
  534.             case 1:
  535.                 return 0x0f;
  536.                 break;
  537.             case 2:
  538.                 return 0x0e;
  539.                 break;
  540.             case 3:
  541.                 return 0x01;
  542.                 break;
  543.             default:
  544.                 return gaplus_customio_3[offset];
  545.         }
  546.     }
  547. }
  548.  
  549. /************************************************************************************
  550. *                                                                                    *
  551. *           Galaga3 custom I/O chips (preliminary)                                    *
  552. *                                                                                    *
  553. ************************************************************************************/
  554.  
  555. READ_HANDLER( galaga3_customio_1_r )
  556. {
  557.     int mode;
  558.  
  559.     mode = gaplus_customio_1[8];
  560.     if (mode == 1)  /* normal mode & test mode */
  561.     {
  562.         switch (offset)
  563.         {
  564.             case 0:
  565.                 return (readinputport( 2 ) >> 4);    /* coin 1 & 2 */
  566.                 break;
  567.             case 1:
  568.                 return (readinputport( 3 ) &0x0f);    /* 1P controls */
  569.                 break;
  570.             case 2:
  571.                 return (readinputport( 3 ) >> 4);    /* 2P controls */
  572.                 break;
  573.             case 3:
  574.                 return (readinputport( 2 ) & 0x0f);    /* start 1 & 2 and button 1 & 2 */
  575.                 break;
  576.             default:
  577.                 return gaplus_customio_1[offset];
  578.         }
  579.     }
  580.     else if (mode == 8)  /* IO tests chip 1 */
  581.     {
  582.         switch (offset)
  583.         {
  584.             case 0:
  585.                 return 0x06;
  586.                 break;
  587.             case 1:
  588.                 return 0x09;
  589.                 break;
  590.             default:
  591.                 return gaplus_customio_1[offset];
  592.         }
  593.     }
  594.     return gaplus_customio_1[offset];
  595. }
  596.  
  597. READ_HANDLER( galaga3_customio_2_r )
  598. {
  599.     int val, mode;
  600.  
  601.     mode = gaplus_customio_2[8];
  602.     if (mode == 5)  /* IO tests chip 2 */
  603.     {
  604.         switch (offset)
  605.         {
  606.             case 0:
  607.             case 1:
  608.                 return 0x0f;
  609.                 break;
  610.             default:
  611.                 return gaplus_customio_2[offset];
  612.         }
  613.     }
  614.     else if (mode == 4)     /* this values are read only by the game on power up */
  615.     {
  616.         switch (offset)
  617.         {
  618.             case 1:
  619.                 val = readinputport( 0 ) & 0x0f;    /* credits/coin 1P & fighters */
  620.                 break;
  621.             case 2:
  622.                 val = readinputport( 1 ) >> 5;        /* bonus life */
  623.                 break;
  624.             case 4:
  625.                 val = readinputport( 1 ) & 0x07;    /* rank */
  626.                 break;
  627.             case 7:
  628.                 val = readinputport( 0 ) >> 6;        /* credits/coin 2P */
  629.                 break;
  630.             default:
  631.                 val = gaplus_customio_2[offset];
  632.         }
  633.         return val;
  634.     }
  635.     else
  636.         return gaplus_customio_2[offset];
  637. }
  638.  
  639. READ_HANDLER( galaga3_customio_3_r )
  640. {
  641.     int mode;
  642.  
  643.     mode = gaplus_customio_3[8];
  644.     if (mode == 2)
  645.     {
  646.         switch (offset)
  647.         {
  648.             case 0:
  649.                 return ((readinputport( 0 ) & 0x20) >> 3) ^ ~(readinputport( 1 ) & 0x08); /* cabinet & test mode */;
  650.                 break;
  651.             case 2:
  652.                 return 0x0f;
  653.                 break;
  654.             default:
  655.                 return gaplus_customio_3[offset];
  656.         }
  657.     }
  658.     else
  659.     {
  660.         switch (offset)
  661.         {
  662.             case 0:
  663.                 return ((readinputport( 0 ) & 0x20) >> 3) ^ ~(readinputport( 1 ) & 0x08); /* cabinet & test mode */;
  664.                 break;
  665.             case 1:
  666.                 return 0x0f;
  667.                 break;
  668.             case 2:
  669.                 return 0x0e;
  670.                 break;
  671.             case 3:
  672.                 return 0x01;
  673.                 break;
  674.             default:
  675.                 return gaplus_customio_3[offset];
  676.         }
  677.     }
  678. }
  679.